package com.nb.customprocessbar;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import androidx.annotation.Nullable;

import java.lang.reflect.Type;
import java.math.BigDecimal;

/**
 * Version 1.0
 * Describe:自定义进度条
 */
class CustomProcessBar extends View {
    private int mLineWidth = 10;//线宽度
    private int mBelowColor = Color.BLUE;//下面的颜色
    private int mAboveColor = Color.RED;//上面的颜色
    private int mTextColor = Color.RED;//文字的颜色
    private int mTextSize = 26;//文字的大小
    private float mProcess = 0.0f;

    private Paint mBelowPaint;
    private Paint mAbovePaint;
    private Paint mTextPaint;

    public CustomProcessBar(Context context) {
        this(context, null);
    }

    public CustomProcessBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomProcessBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CustomProcessBar);
        mLineWidth = (int) array.getDimension(R.styleable.CustomProcessBar_lineWidth, dip2px(mLineWidth));
        mBelowColor = array.getColor(R.styleable.CustomProcessBar_belowColor, mBelowColor);
        mAboveColor = array.getColor(R.styleable.CustomProcessBar_aboveColor, mAboveColor);
        mTextColor = array.getColor(R.styleable.CustomProcessBar_myTextColor, mTextColor);
        mTextSize = array.getDimensionPixelSize(R.styleable.CustomProcessBar_myTextSize, sp2px(mTextSize));//需要转化
        array.recycle();

        mBelowPaint = getMyPaint(mBelowColor);
        mBelowPaint.setStrokeWidth(mLineWidth);
        mAbovePaint = getMyPaint(mAboveColor);
        mAbovePaint.setStrokeWidth(mLineWidth);
        mTextPaint = getMyPaint(mTextColor);
        mTextPaint.setTextSize(mTextSize);


    }

    private int sp2px(int sp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, getResources().getDisplayMetrics());
    }

    private float dip2px(int dip) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, getResources().getDisplayMetrics());
    }


    private Paint getMyPaint(int color) {
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setStyle(Paint.Style.STROKE);

        paint.setColor(color);
        return paint;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);

        if (widthMode == MeasureSpec.AT_MOST) {
            width = 500;//默认
        }
        if (heightMode == MeasureSpec.AT_MOST) {
            height = 500;//默认
        }

        //取小的哪一个 使其成为一个正方形
        setMeasuredDimension(width > height ? height : width, width > height ? height : width);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //super.onDraw(canvas);
        //1.画下面的圆
        int center = getWidth() / 2;
        int radius = center - mLineWidth / 2;

        canvas.drawCircle(center, center, radius, mBelowPaint);

        //2.画上面的圆弧
        RectF rectF = new RectF(mLineWidth / 2, mLineWidth / 2, getWidth() - mLineWidth / 2, getHeight() - mLineWidth / 2);
        canvas.drawArc(rectF, 0, mProcess * 360, false, mAbovePaint);

        //3.画中间的文字
        String text = (int) (mProcess * 100) + "%";
        Rect rect = new Rect();
        mTextPaint.getTextBounds(text, 0, text.length(), rect);
        int x = getWidth() / 2 - rect.width() / 2;
        Paint.FontMetricsInt fontMetrics = mTextPaint.getFontMetricsInt();

        int dy = (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom;
        int baseLine = getHeight() / 2 + dy;

        canvas.drawText(text, x, baseLine, mTextPaint);


    }

    public synchronized void setProcess(float process) {
        this.mProcess = process;
        invalidate();
    }


}
